package demo2.demo2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

//    查询数据库中的所有信息
//    没有实体类，数据库的东西怎么获取？Map
    @GetMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql = "select * from tb_user";
        List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }

    @GetMapping("/addUser")
    public String addUser(){
        String sql = "insert into newsdb.teacher(id,name) values (4,'小朋友')";
        jdbcTemplate.update(sql);
     return "AddOK";
    }
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql = "update newsdb.teacher set name=? where id="+id;
        Object[] objects = new Object[1];
        objects[0] = "小星星";
        jdbcTemplate.update(sql,objects);
        return "UpdateOK";
    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        String sql = "delete from newsdb.teacher where id=?";
        jdbcTemplate.update(sql,id);
        return "DeleteOK";
    }
}

